/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "oboeru_sheet.h"
#include "kanji.h"

int OboeruSheet::
count_pagine ( void )
{
	return ( _kanji->size() / 48 ) + ( ( _kanji->size() % 48 ) > 0 );
}

void OboeruSheet::
set_up_kanji_table ( const Cairo::RefPtr<Cairo::Context>& cr )
{
	cr->select_font_face ( "TakaoGothic", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL );
	cr->set_font_size ( 12.0 );
	cr->set_source_rgb(0.0, 0.0, 0.0);
	cHeight = 95.0;
	cWidth = 80.0;
}

void OboeruSheet::
draw_kanji_grid ( const Cairo::RefPtr<Cairo::Context>& cr, double dy )
{
	cr->set_line_width(2.0);
	cr->move_to (xLeft, yUp + dy);
	cr->line_to (xLeft, pHeight - yDown + dy);
	cr->line_to (pWidth - xRight, pHeight - yDown + dy);
	cr->line_to (pWidth - xRight, yUp + dy);
	cr->line_to (xLeft, yUp + dy);
	for(short i = 1; i < 8; i++)
	{
		cr->move_to (xLeft, yUp + (cHeight * i) + dy);
		cr->line_to (pWidth - xRight, yUp + (cHeight * i) + dy);
	}
	for(short i = 1; i < 6; i++)
	{
		cr->move_to (xLeft + (cWidth * i), yUp + dy);
		cr->line_to (xLeft + (cWidth * i), pHeight - yDown + dy);
	}
	cr->stroke ();
	cr->set_line_width(0.5);
	double unita = cHeight / 7.0;
	for(short i = 0; i < 8; i++)
	{
		cr->move_to (xLeft, yUp + unita + (cHeight * i) + dy);
		cr->line_to (pWidth - xRight, yUp + unita + (cHeight * i) + dy);
		cr->move_to (xLeft, yUp + (5.0 * unita) + (cHeight * i) + dy);
		cr->line_to (pWidth - xRight, yUp + (5.0 * unita) + (cHeight * i) + dy);
		cr->move_to (xLeft, yUp + (6.0 * unita) + (cHeight * i) + dy);
		cr->line_to (pWidth - xRight, yUp + (6.0 * unita) + (cHeight * i) + dy);
	}
	cr->stroke ();
}

void OboeruSheet::
draw_kanji_at_page ( const Cairo::RefPtr<Cairo::Context>& cr, int pagina, bool pages )
{
	double dy = pages ? pHeight * pagina : 0.0;
	double unita = cHeight / 7.0;
	Cairo::TextExtents te;
	list <Kanji>::iterator _itKnj = _kanji->begin();
	list <Kanji>::iterator _itEnd = _kanji->end();
	for ( int nk = 0; nk < ( pagina * 48 ); nk++ )
		_itKnj++;
	for ( short nk = 1; nk < 49; nk++ )
	{
		if ( _itKnj != _itEnd )
		{
			short int _riga = (nk / 6) + ((nk % 6) > 0) - 1;
			short int _colonna = nk - (_riga * 6) - 1;
			Glib::ustring label = get_first_word ( _itKnj->get_significato() );
			cr->get_text_extents ( label.raw(), te );
			double x = xLeft + (_colonna * 80) + ( ( cWidth - te.x_advance ) / 2.0 );
			double y = yUp + dy + (_riga * 95) + te.height + ( ( unita - te.height ) / 2.0 );
			cr->move_to ( x, y );
			cr->show_text ( label.raw() );
			label = get_first_word ( _itKnj->get_kunyomi() );
			cr->get_text_extents ( label.raw(), te );
			x = xLeft + (_colonna * 80) + ( ( cWidth - te.x_advance ) / 2.0 );
			y = yUp + dy + (_riga * 95) + ( 5.0 * unita ) + te.height + ( ( unita - te.height ) / 2.0 );
			cr->move_to ( x, y );
			cr->show_text ( label.raw() );
			label = get_first_word ( _itKnj->get_onyomi() );
			cr->get_text_extents ( label.raw(), te );
			x = xLeft + (_colonna * 80) + ( ( cWidth - te.x_advance ) / 2.0 );
			y = yUp + dy + (_riga * 95) + ( 6.0 * unita ) + te.height + ( ( unita - te.height ) / 2.0 );
			cr->move_to ( x, y );
			cr->show_text ( label.raw() );
			_itKnj++;
		}
		if ( _itKnj == _itEnd )
			break;		
	}
}

Glib::ustring OboeruSheet::
get_first_word ( const Glib::ustring &str )
{
	Glib::ustring trovato;
	size_t _pos = str.find_first_of ( " ,　、", 0 );
	trovato = _pos != Glib::ustring::npos ? str.substr( 0, _pos ) : str;
	return trovato;
}
